<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT . 'model/common/video.php';
require_once APP_ROOT . 'model/common/statdivisions.php';
require_once APP_ROOT . 'model/common/statscores.php';
require_once APP_ROOT . 'model/common/statteams.php';
require_once APP_ROOT . 'model/common/statleagues.php';
require_once APP_ROOT . 'model/common/categories.php';
require_once APP_ROOT . 'model/common/session.php';
require_once APP_ROOT . 'model/common/form.php';
require_once APP_ROOT . 'model/common/user.php';
require_once APP_ROOT . 'model/common/date.php';
require_once APP_ROOT.'include/inc.config.php';


// divisions create/update form
class LeaguesFormController extends FormController {

	var $admin_user = false;
	var $staff_user = false;
	var $view; // current view

	function setUserType($admin_user=false, $staff_user = false) {
		$this->admin_user = $admin_user;
        $this->staff_user = $staff_user;
	}

    function setView(&$view) {
    	$this->view = $view;
    }


	 function LeaguesFormController() {
     	$this->addChild('create', new ButtonController(new Delegate($this, 'create'))); // create
        $this->addChild('save', new ButtonController(new Delegate($this, 'save'))); // save
        $this->addChild('delete', new ButtonController(new Delegate($this, 'delete'))); // delete

        $this->addChild('clean', new ButtonController(new Delegate($this, 'clean'))); // clean
        $this->addChild('leagues', new ButtonController(new Delegate($this, 'back'))); // clean

        $this->setDefaultChild('clean');

        // validation
		$this->addRule(new Handle('RequiredRule', array('leagueName')));
        $this->addRule(new Handle('SizeRangeRule', array('leagueName', 112)));


        $this->registerOnLoadListener(new Delegate($this, 'load'));
        //$this->registerOnActivateListener(new Delegate($this, 'check'));

        $this->addView('delete', new Handle('RedirectView', array('/delete')));

	 }







     function create(&$source, &$request, &$responseModel) {
     	if ($responseModel->isValid()) {
            require_once WACT_ROOT.'/util/phpcompat/clone.php';


            $tz =& new MyTimeZone();
            $responseModel->set('date_added', gmdate("Y-m-d H:i", $tz->timeNowLocal() ) );


            $leagueID = StatLeague::insertLeague($responseModel);
            if ($leagueID) {
            	$responseModel->set('leagueID', $leagueID);
                $lastRecord = clone_obj($responseModel);


                // Set up the model for adding the next record
                $responseModel->removeAll();
                $responseModel->set('LastRecord', $lastRecord);

                return 'createOK';
            } else {
                return 'failure';
            }
        } else {

        }
     }


	 function delete(&$source, &$request, &$responseModel) {
	 	return 'delete';
	 }

	 function enable(&$source, &$request, &$responseModel) {
        $leagueID = $request->getParameter('leagueID');
	 	//print 'Activating Division: '.$divisionID;
	 	if ($leagueID) {
	 		StatLeague::publishLeague($leagueID);
	 		$responseModel->set('enabled_show', 1);
	 	}

	 	return 'this';
	 }

     function disable(&$source, &$request, &$responseModel) {
        $leagueID = $request->getParameter('leagueID');
	 	//print 'DeActivating Division: '.$divisionID;
	 	if ($leagueID) {
	 		StatLeague::denyLeague($leagueID);
	 		$responseModel->remove('enabled_show');
	 	}

	 	return 'this';
	 }



     function save(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$leagueID = $request->getParameter('leagueID');


        	//prepare admin user
            $admin_user_id = 0;
            if (Session::isLogin()) {
            	$admin_user_id = Session::getUserId();
            }
            if ($admin_user_id) $responseModel->set('admin_user_id',$admin_user_id);


            if ($leagueID) StatLeague::updateLeague($leagueID, $responseModel);
            return 'saveOK';
        }
    }

    function clean(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'cleanOK';
    }

    function back(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'index';
    }


    function load(&$source, &$request, &$responseModel) {
     	$leagueID = $request->getParameter('leagueID');
     	//print 'from load: user-id='.$user_id;
     	if ($leagueID) {
        	$record =& StatLeague::getRecord($leagueID);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());

            $enabled = $record->get('enabled');
            if (!$enabled) $responseModel->remove('enabled');
        }
     }

     //prepare the form view
     function prepare(&$source, &$request, &$responseModel, &$template) {
     	//print 'Prepare form...';
     	$leagueID = $request->getParameter('leagueID');
     	if ($this->admin_user) {
     		$responseModel->set('admin_user',1);
     	}
     	if ($this->staff_user) $responseModel->set('staff_user',1);
        $responseModel->set('host',HOST);
        if (!$leagueID) $responseModel->set('enabled',1);


        // determine the current user
        if (Session::isLogin()) {
        	$user_id = Session::getUserId();
        	$isAuthor = User::isUserArticleWriter($user_id);
        	$isAdmin =  User::isUserAdmin($user_id);
        }

        if ($isAuthor) $responseModel->set('author_user',1);



     }



}

?>